package sis.ra.yahoor.result;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;

import sis.ra.utility.Utils;


public class extractPattern {

	public static String stem (String text)
	{
		String linguistxpath="c:\\Program Files\\INXIGHT\\LinguistxPlatform3.7\\lx-3\\";
		String htformat="text";
		Utils.writeFile(linguistxpath+"windows-2003-i686-msvc-7.1-32\\test.txt ", text);
		String s="";
		HashMap<String, Integer> verblist=new HashMap<String, Integer>();
		String cmd="\""+linguistxpath+"windows-2003-i686-msvc-7.1-32\\test_platform.exe\" -k 735442535d524e "+
		"-d " + "\""+linguistxpath + "lang\"" + 
		" -stem " +
		" -"+htformat+" " +"\"" +linguistxpath + "windows-2003-i686-msvc-7.1-32\\test.txt\" ";
		String verb="";
		try {
//			System.out.println(cmd);
		     Process p = Runtime.getRuntime().exec(cmd);
		    
		    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		    while ((s = stdInput.readLine()) != null)   
		    {
//		    	System.out.println(s);
		    	String []ts=s.split("\t");
		    	if (ts.length==2)
		    	{
		    		verb=ts[1].trim();
		    		if (ts[1].contains("|"))
		    		{
		    			verb=ts[1].split("\\|")[0].trim();
		    		}
//		    		System.out.println("++>"+verb);
		    		if (verblist.keySet().contains(verb))
		    		{
		    			Integer count=verblist.get(verb);
		    			verblist.remove(verb);
		    			verblist.put(verb, count+1);
		    		}else
		    			verblist.put(verb, 1);
		    	}
//		    	if (s.startsWith(prefix))
		    }
//		    while ((s = stdError.readLine())!=null)
//		    {
//		    	System.out.println(s);
//		    }
		}
		catch (IOException e) {
			    e.printStackTrace();    
		}
		
		return verb;
	}
	
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.nonverb";//verb";//result1006.snippetCPonly";
		String content=Utils.readFile(filepath);
		String [] lines=content.split("\n");
		String filename="";
		int count1=0;;
		int count2=0;
		int count3=0;
		int count4=0;
		int count5=0;
		int count6=0;
		int count7=0;
		int count8=0;
		int count9=0;
		int count10=0;
		int count11=0;
		HashMap<String, Integer> freq=new HashMap<String, Integer>();
		HashMap<String, Integer> bow=new HashMap<String, Integer>();
		for (int i=0;i<lines.length;i++)
		{
//			System.out.println(lines[i]);
			String line=lines[i];
//			if (line.startsWith("==>")) continue;
			if (line.contains("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\"))
			{
//				System.out.println("files: "+line);
//  			System.out.println(lines[i+1]);
				i=i+1;
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.offset31", line,true,true);
//				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.offset4", line, true, true);
				filename=line;
			}else
			{
				if (line.length()<2) continue;
				int productpos=line.indexOf("PRODUCT");
				int companypos=line.indexOf("COMPANY");
				int offset=0;
				String pattern="";
				if (productpos>companypos)
				{
					offset=line.substring(companypos, productpos).split(" ").length;
					pattern=line.substring(companypos, productpos+7);
				}else
				{
					offset=line.substring(productpos, companypos).split(" ").length;
					pattern=line.substring(productpos, companypos+7);
				}
//				System.out.println(offset);
//				if (offset==1)
//				{
					count1=count1+1;
//					System.out.println("111> "+line);
//					System.out.println("==> "+pattern);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.offset31", line,true,true);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset1", "==> "+line,true,true);
//				}else if (offset==2)
//				{
//					System.out.println(line);
//					System.out.println(pattern);
//					String cont=pattern.replace("COMPANY","").replace("PRODUCT", "").replace("<b>", "").replace("</b>", "").trim();
//					if (cont.equals("")) cont="NULL"; 
//					System.out.println(cont);
//					if (freq.keySet().contains(cont))
//					{
//						int cur=freq.get(cont);
//						freq.remove(cont);
//						freq.put(cont, cur+1);
//					}else
//					{
//						freq.put(cont, 1);
//					}
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset2", line, true,true);
//					count2=count2+1;
//				}
//					else if (offset==3)
//				{
//					System.out.println(" "+line);
//					System.out.println(pattern);
//					String cont=pattern.replace("COMPANY","").replace("PRODUCT", "").replace("<b>", "").replace("</b>", "").trim().toLowerCase();
//					cont=Utils.removePunctuation(cont).trim();
////					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset3", line, true,true);
//					if (cont.equals("")) cont="NULL"; 
//					System.out.println(cont);
//					if (freq.keySet().contains(cont))
//					{
//						int cur=freq.get(cont);
//						freq.remove(cont);
//						freq.put(cont, cur+1);
//					}else
//					{
//						freq.put(cont, 1);
//					}
//					count3=count3+1;
//				}
//				else if (offset==4)
//				{
//					System.out.println("444> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset4", line, true,true);
//					count4=count4+1;
					
//					System.out.println(" "+line);
//					System.out.println(pattern);
//					String cont=pattern.replace("COMPANY","").replace("PRODUCT", "").replace("<b>", "").replace("</b>", "").trim().toLowerCase();
//					cont=Utils.removePunctuation(cont).trim();
////					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset3", line, true,true);
//					if (cont.equals("")) cont="NULL"; 
//					System.out.println(cont);
//					if (freq.keySet().contains(cont))
//					{
//						int cur=freq.get(cont);
//						freq.remove(cont);
//						freq.put(cont, cur+1);
//					}else
//					{
//						freq.put(cont, 1);
//					}
					
//				}
//				else if (offset==5)
//				{
//					System.out.println("555> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset5", line, true,true);
//					count5=count5+1;
//				}
//				else if (offset==6)
//				{
//					System.out.println("666> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset6", line, true,true);
//					count6=count6+1;
//				}
//				else if (offset==7)
//				{
//					System.out.println("777> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset7", line, true,true);
//					count7=count7+1;
//				}
//				else if (offset==8)
//				{
//					System.out.println("888> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset8", line, true,true);
//					count8=count8+1;
//				}
//				else if (offset==9)
//				{
//					System.out.println("999> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset9", line, true,true);
//					count9=count9+1;
//				}
//				else if (offset==10)
//				{
//					System.out.println("000> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset10", line, true,true);
//					count10=count10+1;
//				}
//				else 
//				{
//					System.out.println("===> "+line);
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset11", line, true,true);
//					count11=count11+1;
//				}
//				
//					System.out.println(" "+line);
//					System.out.println(pattern);
					String cont=pattern.replace("COMPANY","").replace("PRODUCT", "").replace("<b>", "").replace("</b>", "").trim().toLowerCase();
					cont=Utils.removePunctuation(cont).trim();
//					Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verboffset3", line, true,true);
					
					if (cont.equals("")) cont="NULL"; 
					System.out.println(cont);
					if (freq.keySet().contains(cont))
					{
						int cur=freq.get(cont);
						freq.remove(cont);
						freq.put(cont, cur+1);
					}else
					{
						freq.put(cont, 1);
					}
					
					String [] terms=cont.split(" ");
					String now="";
					for (int z=0;z<terms.length;z++)
					{
						if (terms[z].length()<2||terms[z].toLowerCase().equals("null"))
							now=terms[z];
						else
							now=stem(terms[z]);	
						System.out.println(now);
						if (bow.keySet().contains(now))
						{													
							System.out.println("=="+now);
							int cur=bow.get(now);
							bow.remove(now);
							bow.put(now, cur+1);
						}else
						{
							bow.put(now, 1);
						}
						
					}
			}
			
		}
		System.out.println(count1+count2+count3+count4+count5+count6+count7+count8+count9+count10+count11+
				"="+count1+" "+count2+" "+count3+" "+count4+" "+count5+" "+count6+" "+count7+" "+count8+" "+count9+" "+count10+" "+count11);
		System.out.println(count3);
		Integer total=0;
//		Iterator<String> it=freq.keySet().iterator();
//		while (it.hasNext())
//		{
//			String term=it.next();
//			Integer count=freq.get(term);
//			total=total+count;
////			if (count>1)
//			System.out.println(term+"\t"+count);
//		}
//		System.out.println(total);
//		total=0;
		Iterator<String> term=bow.keySet().iterator();
		while (term.hasNext())
		{
			String ter=term.next();
			Integer count=bow.get(ter);
			total=total+count;
			
			System.out.println(ter+"\t"+count);
		}
		System.out.println(total);
	}
}
